package com.qf.utils;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;


public class AliPayTools {
    private static String serverUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";//沙箱环境的支付宝网关地址

    private static String appId = "9021000128682611";//应用id

    //个人应用私钥
    private static String  privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCm02Z8ZnoQfalyhITpMsqXD6zXG6/4JZJVPjOEmg7yU7FrT2YHExLE6sDkcJG6PCbhCg48ShZWLEgt9xaehLmu4l3jsDzg1Xad9uF9qBU/qV0HhyPGOjrjHveFFLrUHOZSzY4qGPLjmcrPRF30u1L2Zfx8o3LMudtu4uMoKmeg2MbN8q/AX1E0NVrBiY2cW8yrZCNM0D2AovDCtr+ihMZst0IhUTbuRop8XxoirkZgn7leXtDWGgiOkofyT1wfSPn8qQBNbc8ghHddvmsGoM8QEOWjDHxK571QcKkKZtJVJFcEFokpAOl/OLX1rZDTw4DKgad3rS3xLRvPdfrnzHllAgMBAAECggEBAKWJAxrRfLG8X1U41cGoGoqGjVTtLlEmxus6y1f6nnYqQdRfdrfyXx9yaXDDmnaXlWSb28BLK5raWczyb0lgHffAurVhfScD6a2Y+er7zAndfmgn2oGp6iDP63/sL+6lto3XVgfDtc/OHQZJqolbD1lbnYv3i8RFb8256PlECov4sqHCTDg6toF1efadAwN8Z4dnB3ALawV2RQVR9r2gby/DAqOr3/FXZQ+BdQHdikWpCTKpVEYQLKmMgzLNtTmvIZOBFyTGTgjo2/aIglajO7zqBnw4Wt4G/LX0voXEPkNX8lST5mbx5PVYI6iTJ3sAOmFU5hL7F6ee5hXBJdyUi4ECgYEA4cOau9b+wUI5Yx3ke6yAslBzOONcBmMCM+d84xHTx+nEhbYpGzSC6tPkZF1LuucaBis71ksag0L/v+VObNMmC4k6maMFPlBsqq7ovsBHDOLIpYIg/Xd3zxHP77/mVulsLFH7JZm1/il31NdqiSD9AITPfVEL2VgfP/RNKAysBJUCgYEAvSsTuRKNAxFcAAgkvKbJQhGhdINYiVdFYc2qEktMqWJ+ZA3RGiNpGlYPSOzsJVBlGehy025MWGsmBFLSBoy0MlDvjUrkUp3xyKuCoUNwbp35Oomfm742Hz6JFhdqvmyRot0aJB0KzLH0BTLc79l7TD9OQURxFpvQw2QmXF81HZECgYEAr959smhoKkqZg+HsdH9L/hx3tdA/huYn+CETg5wGm4w6wGLIIN5GQgJwv9KBxQZ0mmuhgG0fKTPPIUDTWCkgUQpE8RwyYHmsPUZmHvqQHrSn027MnkcruM5KapzWJcxOrTPr7j2QrX7GT7LD1jExhe7v59Jz5YslRN/eHx3H/wUCgYAg85gD7Q9TMXrtJn3/vq3gLGILgbWydtlHKCQv+53nA5kwpJeADhsI02TmNx8LgO2bjboyykvSOxfsOOEM8wWQnjicTCdpnMm5QucGYFD9c9cuMzryTjfvSV1n07xsX00fKZScJdLzxSvUm/+QaDnqFjzzcZChd5Lt9kwYWx1NgQKBgEoEESsq6FwEVq5FGe+ktaIqoU5zVysClRm9euSHuNSXCErLGgBEp4UoT/a/ToUffmuQBdNXQL2EbceyKIsIcS0GwG5E20NtC6slMnWpevSiIKjZ+Q9MGshCgHPiISAT1amGFKDEMQ5cZBtJMjJ9A1ftG62AY/dMAx9eEXy40vJG";

    private static String format = "json";//参数的数据格式

    public static String charset = "utf-8";//编码方式

    //支付宝公钥：个人应用私钥--》个人应用公钥--》支付宝公钥
    public static String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjlKItGZuQi7cjSE8a3/hyE19zR/uyJ3ygI8D/a4FuIX1Vg8i90HauLYEP3kkVaS0d4AkE0OUpw2zqoygq9RDcDKrVnFC/dqkWM4JckleVHI41eYXfftNBXg53cJmqNmFkGmzMvpYRGqUO9NctJLQf3oIa9yUjO1BJC4VuvT2cX2JxHne3xJC5s1x21crkvMrXSb0xRXZba8VQ1wmoPV2YAqe+VijRvZpnlYIXp87bxNNPHjau3oVSuM+mQcGF6d0jvxhzBxUQIq/poowMCcBlMKL9/R07Hiq6DieLLMNd4uqMseEEDWqacaBh2zuC94V3UsJcASk8U5sFhqJp1PIUwIDAQAB+gf9l+0lokVvGzSNvQfqKa17tBzAnh10fr42bdEfpoqUQ/EMyEbOHRTnyCOZuJ5NrzKK+7R7nGW2cg6HRJB0mf0hOa6GpXJVmjLJL8+d/QKjJYHMdGkwvffhMB2P1M0ynNcbejobkpcn/5L6lYOHbIIAg5Y9zPhOX3n6Y5f144/9A9kvc82cgoOhWPwXvIztLMu/eylSR3KYPpobFChjmrjol4Hwk+cD8iaP/7c/dwlllwXr6G0bV7P3KloKjfhdACSmYOG02jx7dXsR2wIDAQAB+dPkuAxTw5bkLeLFCpVvo8EB5CH3c4GjbjSQ8V/BxFE8xH4FJ8Lm9TzPx38xJcRYGvJ70E63Z79xIWNdmmGY2zg4XID1CpMiXjtwvTvjwhrGW1nO9CotNJjTwYKr44IkYGdaC9rnFK+McMwS8Mv8ZzEV/KYtmcVjgQG3n+fvcymEKnV65VeCmGFRWimLJ/FZ1UCLuNJNf4FCt3k4N07NrG8D+2M4oVYnu1LBOclXyK78Azx3NBhH/o4ukElHSeeJfSSfxHlJFxV8+UIYEq0tZ0OAgyBriFtrRl5j50SDV/2rmymPzQIDAQAB";

    public static String signType = "RSA2";//验签方式


    public static String pay(){
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,format,charset,alipayPublicKey,signType);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
    //异步接收地址，仅支持http/https，公网可访问
        request.setNotifyUrl("http://t92p6f.natappfree.cc/pay/post");
    //同步跳转地址，仅支持http/https
        request.setReturnUrl("http://localhost:8080/#/OrderList");
    /******必传参数******/
        JSONObject bizContent = new JSONObject();
    //商户订单号，商家自定义，保持唯一性
        bizContent.put("out_trade_no",null);//需要改
    //支付金额，最小值0.01元
        bizContent.put("total_amount",null);//需要改
    //订单标题，不可使用特殊符号
        bizContent.put("subject", "test");
    //电脑网站支付场景固定传值FAST_INSTANT_TRADE_PAY
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

    /******可选参数******/

        try {
        request.setBizContent(bizContent.toString());
        AlipayTradePagePayResponse response = null;
            //获得返回的表单字符串信息；
            String form = alipayClient.pageExecute(request).getBody();
            return form;
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
    }
}
